﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ServiceLayer;
using HelpSystem;

namespace Transportions
{
    public partial class fMain : Form
    {
        CDataBase DataBase = null;

        private bool Connect()
        {
            //Проверяем подключение к БД
            if (DataBase == null)
            {
                DataBase = new CDataBase();
                OnConnectionStateChanged(DataBase.IsOpen ? ConnectionState.Open : ConnectionState.Closed);
                DataBase.StateChanged += new CDataBase.DelegateStateChange(OnConnectionStateChanged);
                return DataBase.IsOpen;
            }
            else
            {
                return DataBase.Reconnect(); 
            }
        }

        public fMain()
        {
            InitializeComponent();
            //Init documentation system
            Documentation.MainForm = this;
            Documentation.LoadErrorCodes("error.xml");
            //Init database conneciton
            Connect();
            //Init WinForms components
            //Transport
            TransportNavigator.BindingSource = DataBase.Transport.TableBindingSource;
            Transport.DataSource = DataBase.Transport.TableBindingSource;
            ((DataGridViewComboBoxColumn)Transport.Columns["transport_type"]).DataSource = DataBase.TransportTypes.TableBindingSource;
            ((DataGridViewComboBoxColumn)Transport.Columns["transport_type"]).DataPropertyName = "transport_type_id";
            ((DataGridViewComboBoxColumn)Transport.Columns["transport_type"]).DisplayMember = "transport_type";
            ((DataGridViewComboBoxColumn)Transport.Columns["transport_type"]).ValueMember = "transport_type_id";
            ((DataGridViewComboBoxColumn)Transport.Columns["transport_type"]).MaxDropDownItems = 5;
        }

        private void OnConnectionStateChanged(ConnectionState NewState)
        {
            switch (NewState)
            {
                case ConnectionState.Open:
                    {
                        sslDataBaseStatus.Text = "Состояние: подключено";
                        sslDataBaseStatus.ForeColor = Color.Green;
                    }
                    break;
                case ConnectionState.Closed:
                    {
                        sslDataBaseStatus.Text = "Состояние: отключено";
                        sslDataBaseStatus.ForeColor = Color.Red;
                        Connect();
                    }
                    break;
            }
        }

        private void miExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void miOptionsDB_Click(object sender, EventArgs e)
        {
            CConfig DBConfig = new CConfig(DataBase);
            if (DBConfig != null)
            {
                DBConfig.Update();
                DBConfig.ShowDialog();
            }
        }

        private void miTransportTypes_Click(object sender, EventArgs e)
        {
            FormTansportType fTransportType = new FormTansportType(DataBase);
            fTransportType.ShowDialog();
        }

        private void fMain_Load(object sender, EventArgs e)
        {
        }

        private void miAbout_Click(object sender, EventArgs e)
        {

        }

        private void miHelp_Click(object sender, EventArgs e)
        {
            Help.ShowHelp(null, "help.chm", HelpNavigator.KeywordIndex, "EC0001");
        }

        private void Pages_Selected(object sender, TabControlEventArgs e)
        {
        }

        private void mmFile_DropDownOpening(object sender, EventArgs e)
        {
            miConnect.Enabled = !DataBase.IsOpen;
        }

        private void miConnect_Click(object sender, EventArgs e)
        {
            Connect();
        }

        private void tpTransport_Click(object sender, EventArgs e)
        {

        }

        private void Transport_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            CTransport transport = new CTransport(DataBase.Transport.Table.Rows[e.RowIndex]);
            transport.ShowDialog(DataBase);
        }
    }
}
